$(".nav-tabs li").click(function(){
    if($(this).index() == 0){
        $("#searchForm").submit();
    }else{
        $("#searchForm2").submit();
    }
});

function changeHotelTime() {
    $("#hotel_inputtask").val("");
    $("#hotel_inputtask").attr("required", false);
}

function changeHotelTaskID() {
    $("#hotel_inputtask").attr("required", true);
}

function saveOneHotel(commodityId,productId,taskId,companyCode) {
    var dateVO = $("#hotel").find("input[name='dateVO']:checked").val();
    var taskID = $("#hotel_inputtask").val();

    bootbox.confirm("<strong style='font-size:15px;padding-left:200px'>确认保存该条数据吗？</strong>", function (result) {
        if (result) {
            $.ajax({
                url: validateProContext + '/validateProduct/saveOneHotel?&dateVO=' + dateVO + '&taskIDVO=' + taskID + "&type = hotel" + "&commodityId=" +commodityId +  "&productId=" + productId +
                "&taskId=" + taskId + "&companyCode=" + companyCode,
                type: "GET",
                async: false,
                dataType: "json",
                success: function (data) {
                    if (data.sessionStatus == 540) {//异步请求session过期状态
                        window.location.href = validateProContext + "/login";
                        return;
                    } else if (data.code == "-1") {
                        bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据保存失败！</strong>", function () {
                            window.location.reload();
                        });
                    } else {
                        bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据保存成功！</strong>", function () {
                            window.location.reload();
                        });
                    }
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据保存失败！</strong>");
                }
            });
        }
    });

}

function updateOneHotel(id) {
    $.ajax({
        url: validateProContext + '/validateProduct/hotelEdit?id=' + id,
        type: "GET",
        success: function (data) {
            if (data.sessionStatus == 540) {//异步请求session过期状态
                window.location.href = validateProContext + "/login";
            } else if (data.code == "1") {
                var hotelVO = data.hotelVO;
                $("#myModal").modal("show");
                $('#commodity_name').val(hotelVO.commodityName);//商品名称
                $('#sale_price').val(hotelVO.salePrice);//售价
                $("#detail_id").val(hotelVO.id);
            }
        }
    });
}

function hotelAdd() {
    var detail_id = $("#detail_id").val();
    var dateVO = $("#hotel").find("input[name='dateVO']:checked").val();
    var taskID = $("#hotel_inputtask").val();
    var commodity_name = $("#commodity_name").val();
    var sale_price = $("#sale_price").val();
    var companyCode = $("#companyCode").val();
    $.ajax({
        url: validateProContext + '/validateProduct/hotelAdd?id=' + detail_id + '&dateVO=' + dateVO + '&taskIDVO=' + taskID + '&commodityName=' + commodity_name + '&salePrice=' + sale_price + '&companyCode=' + companyCode + "&type = hotel",
        type: "GET",
        async: false,
        dataType: "json",
        success: function (data) {
            if (data.sessionStatus == 540) {//异步请求session过期状态
                window.location.href = validateProContext + "/login";
                return;
            } else if (data.code == "-1") {
                bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据修改失败！</strong>", function () {
                    window.location.reload();
                });
            } else {
                bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据修改成功！</strong>", function () {
                    window.location.reload();
                });
            }
        }, error: function (XMLHttpRequest, textStatus, errorThrown) {
            bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据修改失败！</strong>");
        }
    });
}

function deleteOneHotel(commodityId,productId,taskId,companyCode) {
    var dateVO = $("#hotel").find("input[name='dateVO']:checked").val();
    var taskID = $("#hotel_inputtask").val();
    bootbox.confirm("<strong style='font-size:15px;padding-left:200px'>确认删除该条数据吗？</strong>", function (result) {
        if (result) {
            $.ajax({
                url: validateProContext + '/validateProduct/deleteOneHotel?&dateVO=' + dateVO + '&taskIDVO=' + taskID + "&type=hotel" + "&commodityId=" +commodityId +  "&productId=" + productId +
                "&taskId=" + taskId + "&companyCode=" + companyCode,
                type: "GET",
                async: false,
                success: function (data) {
                    if (data.sessionStatus == 540) {//异步请求session过期状态
                        window.location.href = validateProContext + "/login";
                        return;
                    }
                    bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据删除成功！</strong>", function () {
                        window.location.reload();
                    });
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据删除失败！</strong>");
                }
            });
        }
    });

}


function hotel_save() {
    var dateVO = $("#hotel").find("input[name='dateVO']:checked").val();
    var taskID = $("#hotel_inputtask").val();
    var companyCode = $("#companyCode").val();
    var hotelList = [];
    $("tbody tr input[type=checkbox]:checked").each(function(index){
        var commodityId = $(this).parents("tr").children().eq(1).text();
        var productId = $(this).parents("tr").children().eq(3).text();
        var taskId = $(this).parents("tr").children().eq(4).text();
        var obj = {};
        obj.commodityId = commodityId;
        obj.productId = productId;
        obj.taskId = taskId;
        hotelList.push(obj);
    });
    if (dateVO == "taskID") {
        if (taskID == "") {
            bootbox.alert("<strong style='font-size:15px;padding-left:200px'>请填写 taskID!</strong>");
            return false;
        }
    }
    bootbox.confirm("<strong style='font-size:15px;padding-left:200px'>确认保存所有数据吗？</strong>", function (result) {
        if (result) {
            $.ajax({
                url: validateProContext + '/validateProduct/HotelSaveBatch?dateVO=' + dateVO + '&taskIDVO=' + taskID + "&companyCode=" + companyCode + "&jsonData=" + JSON.stringify(hotelList),
                type: "GET",
                async: false,
                success: function (data) {
                    if (data.sessionStatus == 540) {//异步请求session过期状态
                        window.location.href = validateProContext + "/login";
                        return;
                    }
                    bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据保存成功！</strong>", function () {
                        window.location.reload();
                    });
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据保存失败！</strong>", function () {
                        window.location.reload();
                    });
                }
            });
        }
    });

}


function hotel_delete() {
    var dateVO = $("#hotel").find("input[name='dateVO']:checked").val();
    var taskID = $("#hotel_inputtask").val();
    var hotelList = [];
    var companyCode = $("#companyCode").val();
    $("tbody tr input[type=checkbox]:checked").each(function(index){
        var commodityId = $(this).parents("tr").children().eq(1).text();
        var productId = $(this).parents("tr").children().eq(3).text();
        var taskId = $(this).parents("tr").children().eq(4).text();
        var obj = {};
        obj.commodityId = commodityId;
        obj.productId = productId;
        obj.taskId = taskId;
        hotelList.push(obj);
    });
    if (dateVO == "taskID") {
        if (taskID == "") {
            bootbox.alert("<strong style='font-size:15px;padding-left:200px'>请填写 taskID!</strong>");
            return false;
        }
    }
    bootbox.confirm("<strong style='font-size:15px;padding-left:200px'>确认删除所有数据吗？</strong>", function (result) {
        if (result) {
            $.ajax({
                url: validateProContext + '/validateProduct/HotelDeleteBatch?dateVO=' + dateVO + '&taskIDVO=' + taskID + "&companyCode=" + companyCode + "&jsonData=" + JSON.stringify(hotelList),
                type: "GET",
                async: false,
                success: function (data) {
                    if (data.sessionStatus == 540) {//异步请求session过期状态
                        window.location.href = validateProContext + "/login";
                        return;
                    }
                    bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据删除成功！</strong>", function () {
                        window.location.reload();
                    });

                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    bootbox.alert("<strong style='font-size:15px;padding-left:200px'>数据删除失败！</strong>");
                }
            });
        }
    });

}

var active_class = 'active';
$('#dynamic-table2 > thead > tr > th input[type=checkbox]').eq(0).on('click', function(){
    var th_checked = this.checked;//checkbox inside "TH" table header

    $(this).closest('table').find('tbody > tr').each(function(){
        var row = this;
        if(th_checked) $(row).addClass(active_class).find('input[type=checkbox]').eq(0).prop('checked', true);
        else $(row).removeClass(active_class).find('input[type=checkbox]').eq(0).prop('checked', false);
    });
});

//select/deselect a row when the checkbox is checked/unchecked
$('#dynamic-table2').on('click', 'td input[type=checkbox]' , function(){
    var $row = $(this).closest('tr');
    if($row.is('.detail-row ')) return;
    if(this.checked) $row.addClass(active_class);
    else $row.removeClass(active_class);
});
